<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>getSnapshotBeforeUpdate</title>
</head>
<body>
  <div id="test"></div>

  <script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
  <script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
  <script src="https://cdn.staticfile.org/prop-types/15.6.1/prop-types.js"></script>

  <script type="text/babel">
    class Count extends React.Component {
      constructor(props) {
        super(props)
        console.log('constructor')
      }

      add = () => {
        let { count } = this.state
        this.setState({
          count: count + 1
        })
      }

      death = () => {
        React.unMountComponentAtNode(document.getElementById('test'))
      }

      render() {
        console.log('render')
        return (
          <div>
            <p>求和：{this.state.count}</p>
            <button onClick={this.add}>点击加1</button>
            <button onClick={this.death}>卸载组件</button>
          </div>
        )
      }

      state = { count: 0 }

      // 得到一个派生的状态  state的值在任何时候都取决于props时，用这个   使用场景极其罕见
      static getDerivedStateFromProps(props, state) {
        // console.log(props, 'props')
        // console.log(state, 'state')
        console.log('getDerivedStateFromProps')
        return null

      }

      // 在更新之前获取快照
      getSnapshotBeforeUpdate() {
        console.log('getSnapshotBeforeUpdate')
        return 'hahah'    // 返回的这个值，会传给componentDidUpdate
      }

      componentDidMount() {
        console.log('componentDidMount')
      }

      shouldComponentUpdate() {
        console.log('shouldComponentUpdate')
        return true
      }

      componentDidUpdate(preProps, preState, snapshotValue) {
        console.log('ComponentDidUpdate', preProps, preState, snapshotValue)
      }
    }
    ReactDOM.render(<Count count={199}/>, document.getElementById('test'))
  </script>
</body>
</html>